<template>
  <div style="padding: 10px 15px" class="all">
    <!-- 查询条件-->
    <div class="filter-container">
      <el-select
        v-if="this.logincpnid == 'system'"
        v-model="selectparms.cpnID"
        filterable
        clearable
        class="filter-item"
        placeholder="请选择企业"
        @change="CpnselectFn($event)"
        :filter-method="cpnmatch"
        @clear="cpnmatch"
      >
        <el-option
          v-for="item in cpnoptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span v-html="item.label" />
        </el-option>
      </el-select>
      <el-select
        v-model="selectparms.act"
        filterable
        clearable
        class="filter-item"
        style="width: 350px; margin-right: 10px"
        placeholder="请选择收款账户"
        :filter-method="cpnactmatch"
        @clear="cpnactmatch"
      >
        <el-option
          v-for="item in cpnactlstoptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span v-html="item.label" />
        </el-option>
      </el-select>
      <el-col :span="3" style="margin-right: 10px">
        <el-date-picker
          v-model="selectparms.date"
          type="date"
          placeholder="请选择账单日期"
          style="width: 100%; margin-right: 10px"
          format="yyyy-MM-dd"
        />
      </el-col>
      <el-col :span="3" style="margin-right: 10px">
        <el-date-picker
          v-model="selectparms.edate"
          type="date"
          placeholder="请选择账单日期"
          style="width: 100%; margin-right: 10px"
          format="yyyy-MM-dd"
        />
      </el-col>
      <el-button
        class="filter-item seachbttn"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
      <el-button
        class="filter-item seachbttn"
        type="primary"
        @click="handleFilter(2)"
      >
        清空
      </el-button>
    </div>
    <div
      class="filter_fenge"
      style="border: none; border-top: 1px solid #d1ddeb"
    />
    <div class="filter-container">
      <el-button
        :loading="downloadLoading"
        class="filter-item bttnleft"
        type="primary"
        icon="el-icon-download"
        @click="downloadRcBatch(0)"
        >批量下载第三方平台对账单</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-upload"
        @click="openExportdialog()"
        >三方账单导入</el-button
      >
      <!-- <el-button

        type="primary"
        icon="el-icon-upload"

        >上传第三方平台对账单</el-button> -->
    </div>
    <!--查询条件 end-->

    <!--导入三方订单-->
    <el-dialog
      title="导入三方订单"
      :visible.sync="dialogExportVisible"
      width="65%"
    >
      <div
        v-loading="exportloading"
        class="exportotherpaydiv"
        style="display: flex"
      >
        <div class="leftBox" style="width: 40%">
          <el-form :model="export_form" :inline="true" label-position="right">
            <el-form-item
              label="企业号"
              prop="cpnID"
              :rules="[
                {
                  required: true,
                  message: '企业号不能为空',
                  trigger: 'change',
                },
              ]"
            >
              <el-select
                v-model="export_form.cpnID"
                filterable
                clearable
                class="filter-item"
                placeholder="请选择企业"
                style="width: 80%"
                @change="selectpaychannel()"
              >
                <el-option
                  v-for="item in cpnoptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="支付通道"
              prop="paychannel"
              :rules="[
                {
                  required: true,
                  message: '支付通道不能为空',
                  trigger: 'change',
                },
              ]"
            >
              <el-select
                v-model="export_form.paychannel"
                clearable
                filterable
                class="filter-item"
                placeholder="请选择支付通道"
                style="width: 80%"
                @change="selectact()"
              >
                <el-option
                  v-for="item in paytychanneloptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="对账日期"
              prop="dt"
              :rules="[
                { required: true, message: '日期不能为空', trigger: 'blur' },
              ]"
            >
              <el-date-picker
                v-model="export_form.dt"
                style="width: 75%"
                type="date"
                placeholder="请选择日期"
                format="yyyy-MM-dd"
                @change="dateChange"
              />
            </el-form-item>

            <br />
            <!-- <el-form-item
            label="账户"
            prop="act"
            :rules="[
              { required: true, message: '账户不能为空', trigger: 'blur' },
            ]"
          >
            <el-select
              v-model="export_form.act"
              clearable
              filterable
              class="filter-item"
              placeholder="请选择账户"
              style="width: 80%"
            >
              <el-option
                v-for="item in actlist"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item> -->
            <!-- <el-form-item
            label="选择对账日期1"
            prop="dt"
            :rules="[
              { required: true, message: '日期不能为空', trigger: 'blur' },
            ]"
          >
            <el-date-picker
              v-model="export_form.dt"
              style="width: 80%"
              type="date"
              placeholder="请选择日期"
              format="yyyy-MM-dd"
            />
          </el-form-item> -->

            <div style="width: 100%; text-align: left" class="upload-box">
              <el-upload
                ref="upload"
                class="upload-otherpay"
                action=""
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                accept=".csv,.xls,.xlsx"
                :before-upload="beforeUpload"
                multiple
                :limit="100"
                :on-exceed="handleExceed"
                :file-list="fileList"
                :auto-upload="false"
                :on-change="fileChange"
              >
                <el-button
                  type="primary"
                  icon="el-icon-circle-plus-outline"
                  plain
                  @click="RegSubmit"
                  style="width: 100%"
                  title=" 可以按住Ctrl和Shift选择多个文件(账单文件为原始渠道账单文件,不可自定义文件上传)"
                  >选择本地三方账单文件</el-button
                >
              </el-upload>
            </div>
          </el-form>
        </div>
        <div class="rightBox" style="flex: 1">
          <div class="checkBoard">
            <div class="board-title">
              <div class="left-title">支付账号</div>
              <div class="center-title">对应文件</div>
              <div class="right-title">校验结果</div>
            </div>
            <ul class="adminList">
              <li v-for="(item, index) in export_form.uploadfiles" :key="index">
                <span class="item">{{ item.label }}</span>
                <span
                  class="item"
                  v-show="item.filename !== '' && item.type == 1"
                  >{{ item.filename }}</span
                >
                <span class="item" style="float: right">
                  <!-- 公司为兴勤以及选择为京东支付时开启账户验证 -->
                  <template
                    v-if="
                      export_form.cpnID === '2000000275' &&
                      export_form.paychannel === '00014'
                    "
                  >
                    <template v-if="item.type === 0"> 未选择文件 </template>
                    <template v-if="item.type === 1">
                      <span style="color: green">文件校验成功 </span>
                    </template>
                    <template v-if="item.type === 2">
                      <span style="color: red"> 日期校验失败 </span>
                    </template>
                  </template>
                </span>
              </li>
            </ul>
            <div class="boardFoot">
              <el-button
                type="primary"
                @click="submitUpload()"
                style="display: inline-block"
                title="将文件上传至服务器保存,用于支付对账"
                >上传至服务器</el-button
              >
            </div>
          </div>
        </div>

        <!-- <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitUpload()">确 定</el-button>
        </div> -->
      </div>
    </el-dialog>

    <!--弹出层 end-->

    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="HeartbeatData"
      fit
      highlight-current-row
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" fixed="left" />
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column label="企业名称" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.cpnid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款账户" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.act }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="账单日期"
        align="left"
        width="160"
        min-width="180px"
      >
        <template slot-scope="{ row }">
          <span>{{ row.dtStr }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="130">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="downloadRc(scope.row)"
            >下载当日账单</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="listQuery.total > 0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData(this)"
    />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { AjaxToken, TipConfirm, AjaxUpload } from "@/api/common";
import { SelectAjax } from "@/api/common";
import { Jurisdiction } from "@/api/common";
import sessionStorage from "@/api/Storage";
import "url-search-params-polyfill"; // 兼容IE，否则无法识别URLSearchParams
import commconfig from "@/comm.json";
import PinYinMatch from "pinyin-match";

export default {
  name: "Rcncbtch",
  components: { Pagination },
  data() {
    return {
      HeartbeatData: [],
      filebox: [], // 文件盒子
      box: [],
      // adminShow: false,
      cpnoptions: [],
      cpnactlstoptions: [],
      logincpnid: "",
      multipleSelection: [],
      paytychanneloptions: [],
      actlist: [],
      fileList: [],
      formLabelWidth: "120px",
      RcncbtchData: null,
      downloadLoading: false,
      downloadRcERPLoading: false,
      downloadRcBatchLoading: false,
      downloadRcBatchERPLoading: false,
      dialogExportVisible: false,
      exportloading: false,
      listQuery: {
        title: "",
        total: 0,
        page: 1,
        limit: 10,
      },
      selectparms: {
        cpnID: "",
        act: "",
        date: "",
        edate: "",
      },
      export_form: {
        cpnID: "",
        act: "",
        paychannel: "",
        dt: "",
        uploadfiles: [],
        selectfiles: [],
        forecahfiles: [],
      },
      listLoading: false,
      dialogFormVisible: false,
      dialogdetailsVisible: false,
      dialogStatus: "",
      textMap: {
        update: "修改",
        create: "添加",
      },
    };
  },

  // 初始化
  created() {
    var userinfo = JSON.parse(sessionStorage.getItem("userinfo"));
    this.logincpnid = userinfo.cpnId;
    backstage.GeCpntListItems(this);
    if (this.logincpnid != "system") {
      this.cpnactlstoptions = [];
      this.selectparms.cpnID = this.logincpnid;
      this.selectparms.act = "";
      backstage.GetCpnActList(this);
    }
  },
  watch: {},
  mounted() {
    this.export_form.dt = new Date().getTime() - 86400000;
    this.timeErrorMsg = this.debounce(this.timeErrorMsg, 500);
    this.delAdminMsg = this.debounce(this.delAdminMsg, 500);
    this.formatError = this.debounce(this.formatError, 500);
    this.delExtraMsg = this.debounce(this.delExtraMsg, 500);
  },
  methods: {
    cpnmatch(val) {
      if (val) {
        let result = [];
        this.cpnoptions.forEach((item) => {
          let matchRes = PinYinMatch.match(item.label, val);
          if (matchRes) {
            result.push(item);
          }
        });
        this.cpnoptions = result;
      } else {
        this.cpnid = "";
        this.cpnoptions = backstage.GeCpntListItems(this);
      }
    },
    cpnactmatch(val) {
      if (val) {
        let result = [];
        this.cpnactlstoptions.forEach((item) => {
          let matchRes = PinYinMatch.match(item.label, val);
          if (matchRes) {
            result.push(item);
          }
        });
        this.cpnactlstoptions = result;
      } else {
        this.cpnactlstoptions = backstage.GetCpnActList(this);
      }
    },

    submitUpload() {
      var that = this;
      if (this.export_form.cpnID == "") {
        this.$message({
          type: "error",
          duration: 1000,
          message: "请选择企业!",
        });
        return;
      }
      if (this.export_form.paychannel == "") {
        this.$message({
          type: "error",
          duration: 1000,
          message: "请选择支付通道!",
        });
        return;
      }
      // 原代码
      // if (this.export_form.act == "") {
      //   this.$message({
      //     type: "error",
      //     duration: 1000,
      //     message: "请选择账户!",
      //   });
      //   return;
      // }

      if (this.export_form.dt == "") {
        this.$message({
          type: "error",
          duration: 1000,
          message: "请选择日期!",
        });
        return;
      }

      // 原提交代码
      // setTimeout(function () {
      //   if (that.$refs.upload.$children[0].fileList.length == 1) {
      //     that.$refs.upload.submit();
      //   } else {
      //     that.$message({
      //       type: "error",
      //       showClose: true,
      //       duration: 3000,
      //       message: "请选择文件!",
      //     });
      //   }
      // }, 100);

      // 现提交代码
      setTimeout(function () {
        if (that.$refs.upload.$children[0].fileList.length >= 1) {
          that.$refs.upload.submit();
          // 点击提交关闭弹窗
          // that.adminShow = false;
        } else {
          that.$message({
            type: "error",
            showClose: true,
            duration: 3000,
            message: "请选择文件!",
          });
        }
      }, 100);
    },
    // 点击上传三方订单
    RegSubmit() {
      if (this.export_form.dt == "" || this.export_form.dt == null) {
        this.$message({
          type: "error",
          showClose: true,
          duration: 3000,
          message: "请选择对账日期!",
        });
      }
      if (this.export_form.cpnID == "" || this.export_form.cpnID == null) {
        this.$message({
          type: "error",
          showClose: true,
          duration: 3000,
          message: "请选择企业号!",
        });
      }
      if (
        this.export_form.paychannel == "" ||
        this.export_form.paychannel == null
      ) {
        this.$message({
          type: "error",
          showClose: true,
          duration: 3000,
          message: "请选择支付通道!",
        });
      }
    },
    formatError() {
      this.$message({
        duration: 3000,
        message: "部分文件名格式不正确，我们已经帮您删除",
      });
    },
    timeErrorMsg() {
      this.$message({
        duration: 3000,
        message: "部分文件多余且时间不符,我们已经帮您删除掉",
      });
    },
    delAdminMsg() {
      this.$message({
        duration: 3000,
        message: "部分文件账号不符，已经帮您删除掉",
      });
    },
    delExtraMsg() {
      this.$message({
        duration: 3000,
        message: "您上传的文件在文件列表中已经拥有,已经帮您自动删除",
      });
    },
    //func 要执行的函数，interval要等待的时间 节流函数
    throttle(func, interval) {
      let timeout;
      let startTime = new Date();
      return function () {
        clearTimeout(timeout);
        let curTime = new Date();
        if (curTime - startTime <= interval) {
          //小于规定时间间隔时，用setTimeout在指定时间后再执行
          timeout = setTimeout(() => {
            func();
          }, interval);
        } else {
          //重新计时并执行函数
          startTime = curTime;
          func();
        }
      };
    },
    /**
     * @desc 防抖函数
     * @param {需要防抖的函数} func
     * @param {延迟时间} wait
     * @param {是否立即执行} immediate
     */
    debounce(func, delay = 300, immediate = false) {
      let timer = null;
      return function () {
        if (timer) {
          clearTimeout(timer);
        }
        if (immediate && !timer) {
          func.apply(this, arguments);
        }
        timer = setTimeout(() => {
          func.apply(this, arguments);
        }, delay);
      };
    },

    fileChange(file, fileList) {
      this.filebox = fileList;
      // console.log("fileList;: ", fileList);
      // if (
      //   this.filebox.length > 0 &&
      //   this.export_form.dt !== "" &&
      //   this.export_form.dt !== null &&
      //   this.export_form.paychannel !== "" &&
      //   this.export_form.paychannel !== null
      // ) {
      //   // this.adminShow = true;
      // }
      if (
        this.export_form.cpnID === "2000000275" &&
        this.export_form.paychannel === "00014"
      ) {
        var fileobj = {
          act: file.name.slice(0, 9),
          date: file.name.slice(10, 18),
          filename: file.name,
          type: 0,
        };

        var checkfile = [];
        var isfle = false;

        // console.log("fileobj: ", fileobj);
        this.export_form.uploadfiles.forEach((item) => {
          if (fileobj.act == item.act && item.type !== 1) {
            isfle = true;
            if (
              fileobj.date == this.dateFormat(this.export_form.dt) &&
              fileobj.filename.length === 22
            ) {
              //账户时间都正确并且不是副本
              checkfile.push({
                act: item.act,
                date: fileobj.date,
                filename: fileobj.filename,
                type: 1,
                label: item.label,
              });
            } else {
              //账户正确 时间不正确
              checkfile.push({
                act: fileobj.act,
                date: "",
                filename: fileobj.filename,
                label: item.label,
                type: 2,
              });

              // fileList.forEach((result, index) => {
              //   if (fileobj.filename == result.name) {
              //     fileList.splice(index, 1);
              //     this.$message({
              //       duration: 2000,
              //       message: "部分文件多余且时间不符,我们已经帮您删除掉",
              //     });
              //   }
              // });
              for (var i = 0; i < fileList.length; i++) {
                if (fileobj.filename == fileList[i].name) {
                  fileList.splice(i, 1);
                  i--;
                  this.timeErrorMsg();
                }
              }
            }
          } else {
            checkfile.push(item);
          }
        });
        // 删除与账号不符合的文件列表
        var s = [];
        this.export_form.uploadfiles.forEach((item) => {
          s.push(item.act);
        });
        if (s.indexOf(fileobj.act) == -1) {
          for (var i = 0; i < fileList.length; i++) {
            if (fileList[i].name.slice(0, 9) == fileobj.act) {
              fileList.splice(i, 1);
              i--;
              this.delAdminMsg();
            }
          }
          // fileList.forEach((item, index) => {
          //   if (item.name.slice(0, 9) == fileobj.act) {
          //     fileList.splice(index, 1);
          //     this.$message({
          //       duration: 2000,
          //       message: "部分文件账号不符，已经帮您删除掉",
          //     });
          //   }
          // });
        }

        fileList.forEach((item, index) => {
          // if (
          //   s.includes(item.name.slice(0, 9)) &&
          //   item.name.slice(10, 18) !== this.dateFormat(this.export_form.dt)
          // ) {
          //   this.$message({
          //     duration: 2000,
          //     message: "部分文件多余且时间不符,我们已经帮您删除掉",
          //   });

          //   fileList.splice(index, 1);
          // }

          // 修复上传正确文件副本bug
          if (item.name && item.name.length !== 22) {
            fileList.splice(index, 1);
            this.formatError();
          }
        });
        // 删除账号相符但是时间不符的多余文件
        for (var i = 0; i < fileList.length; i++) {
          if (
            s.includes(fileList[i].name.slice(0, 9)) &&
            fileList[i].name.slice(10, 18) !==
              this.dateFormat(this.export_form.dt)
          ) {
            this.timeErrorMsg();

            fileList.splice(i, 1);
            i--;
          }
        }

        // console.log("fileList: ", fileList);
        // 删除多余的账户时间都正确文件
        for (var i = 0; i < fileList.length; i++) {
          var cur = fileList[i];
          for (var j = i + 1; j < fileList.length; j++) {
            if (cur.name === fileList[j].name) {
              fileList.splice(j, 1);
              j--;
              this.delExtraMsg();
            }
          }
        }

        // console.log(
        //   "this.export_form.uploadfiles: ",
        //   this.export_form.uploadfiles
        // );
        this.export_form.uploadfiles = checkfile;
      }
    },

    dateChange(e) {
      // if (
      //   this.export_form.dt !== "" &&
      //   this.export_form.dt !== null &&
      //   this.export_form.paychannel !== "" &&
      //   this.export_form.paychannel !== null
      // ) {
      //   // this.adminShow = true;
      // }
      if (
        this.filebox.length > 0 &&
        this.export_form.dt !== "" &&
        this.export_form.dt !== null &&
        this.export_form.paychannel !== "" &&
        this.export_form.paychannel !== null
      ) {
        this.export_form.uploadfiles.forEach((item) => {
          if (item.type !== 0) {
            if (item.date == this.dateFormat(this.export_form.dt)) {
              item.type = 1;
            } else {
              item.type = 2;
            }
          }
        });
      }
    },

    dateFormat(e) {
      const date = new Date(e);
      const y = date.getFullYear();
      let m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      const time = String(y) + String(m) + String(d);
      return time;
    },
    beforeUpload(file) {
      var that = this;
      that.$refs.upload.clearFiles();
      // 文件类型
      var fileName = file.name.substring(file.name.lastIndexOf(".") + 1);
      if (fileName != "xls" && fileName != "xlsx" && fileName != "csv") {
        that.$message({
          type: "error",
          showClose: true,
          duration: 3000,
          message: "文件类型不是.xls/.xlsx/.csv文件!",
        });
        return false;
      }

      if (this.export_form.cpnID == "" || this.export_form.cpnID == null) {
        console.log("this.export_form.cpnID: ", this.export_form.cpnID);
        that.$message({
          type: "error",
          showClose: true,
          duration: 3000,
          message: "请选择企业",
        });
        return false;
      }
      if (
        this.export_form.paychannel == "" ||
        this.export_form.paychannel == null
      ) {
        that.$message({
          type: "error",
          showClose: true,
          duration: 3000,
          message: "请选择支付通道",
        });
        return false;
      }
      // 原代码
      // if (this.export_form.act == "") {
      //   that.$message({
      //     type: "error",
      //     showClose: true,
      //     duration: 3000,
      //     message: "请选择账户",
      //   });
      //   return false;
      // }
      if (this.export_form.dt == "" || this.export_form.dt == null) {
        that.$message({
          type: "error",
          showClose: true,
          duration: 3000,
          message: "请选择日期",
        });
        return false;
      }
      // 读取文件大小
      // var fileSize = file.size;
      // console.log(fileSize);
      // if (fileSize > 1048576) {
      //   that.exportloading = false;
      //   that.$message({
      //     type: "error",
      //     showClose: true,
      //     duration: 3000,
      //     message: "文件大于1M!",
      //   });
      //   return false;
      // }
      const params = new FormData();
      params.append("file", file);
      params.append("cpnID", this.export_form.cpnID);
      params.append("act", this.export_form.act);
      params.append("paychannel", this.export_form.paychannel);
      params.append("dt", new Date(this.export_form.dt).toLocaleDateString());
      params.append("_t1", new Date());
      that.exportloading = true;
      backstage.beforeUpload(that, params);
      backstage.GetCpnActListSelectListItems(
        that,
        that.export_form.cpnID,
        that.export_form.paychannel
      );
    },
    openExportdialog() {
      this.dialogExportVisible = true;
      var that = this;
      setTimeout(function () {
        that.$refs.upload.clearFiles();
      }, 100);
    },
    selectpaychannel() {
      this.export_form.uploadfiles = [];
      this.export_form.paychannel = "";
      this.export_form.act = "";
      this.actlist = [];
      this.paytychanneloptions = [];
      backstage.GetPyChannelSelectListItems(this, this.export_form.cpnID);

      if (
        this.export_form.dt !== "" &&
        this.export_form.dt !== null &&
        this.export_form.paychannel !== "" &&
        this.export_form.paychannel !== null
      ) {
        this.boxChange();
        // this.adminShow = true;
      }

      // console.log(this.export_form.cpnID);
    },
    selectact() {
      this.export_form.uploadfiles = [];
      this.export_form.act = "";
      this.actlist = [];
      backstage.GetCpnActListSelectListItems(
        this,
        this.export_form.cpnID,
        this.export_form.paychannel
      );

      // if (
      //   this.export_form.dt !== "" &&
      //   this.export_form.dt !== null &&
      //   this.export_form.paychannel !== "" &&
      //   this.export_form.paychannel !== null
      // ) {
      //   this.adminShow = true;
      // }

      if (
        this.export_form.cpnID === "2000000275" &&
        this.export_form.paychannel === "00014"
      ) {
        this.$refs.upload.clearFiles();
        this.$message({
          showClose: true,
          duration: 3000,
          message: "为保证校验正确性,已帮您重置文件列表",
        });
      }
    },
    handleRemove(file, fileList) {
      // 如果是兴勤的京东支付删除文件会更新校验结果
      if (
        this.export_form.cpnID === "2000000275" &&
        this.export_form.paychannel === "00014"
      ) {
        this.export_form.uploadfiles.forEach((item, index) => {
          if (file !== "" && file !== null && file !== undefined) {
            if (file.name.slice(0, 9) == item.act) {
              this.export_form.uploadfiles[index].type = 0;
              this.export_form.uploadfiles[index].filename = "";
            }
          }
        });
      }
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {},
    beforeRemove(file, fileList) {
      // return this.$confirm(`确定移除 ${file.name}？`);
    },
    CpnselectFn(e) {
      this.cpnactlstoptions = [];
      this.selectparms.cpnID = e;
      this.selectparms.act = "";
      backstage.GetCpnActList(this);
    },
    // 弹层关闭
    handleClose() {},
    // 权限
    Jurisdiction(moduleActionName) {
      return Jurisdiction(moduleActionName, this);
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 多选赋值
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    // 搜索
    handleFilter(typ) {
      if (typ == 2) {
        this.listQuery.page = 1;
        this.listQuery.title = "";
        this.selectparms = {
          cpnID: "",
          act: "",
          date: "",
          edate: "",
        };
        this.fetchData();
      } else {
        this.listQuery.page = 1;
        this.fetchData();
      }
    },
    // 查询
    fetchData() {
      var params = new URLSearchParams();
      params.append(
        "Parms",
        '{"page":' +
          this.listQuery.page +
          ',"rows":' +
          this.listQuery.limit +
          "}"
      );
      if (this.selectparms.date == "") {
        this.$message({
          showClose: true,
          message: "请选择日期",
          type: "error",
        });
        return;
      }
      if (this.selectparms.edate == "") {
        this.$message({
          showClose: true,
          message: "请选择结束日期",
          type: "error",
        });
        return;
      }

      var ed = new Date(new Date(this.selectparms.date).toLocaleDateString());
      var nd = new Date(new Date(this.selectparms.edate).toLocaleDateString());
      var now = new Date(new Date().toLocaleDateString());
      if(ed>nd){
        this.$message({
          showClose: true,
          message: "开始日期不能大于结束日期",
          type: "error",
        });
        return;
      }
      if(nd>now){
        this.$message({
          showClose: true,
          message: "只能下载昨日账单",
          type: "error",
        });
        return;
      }
      if (this.selectparms.cpnID == "") {
        this.$message({
          showClose: true,
          message: "请选择企业",
          type: "error",
        });
        return;
      }
      if (this.selectparms.act == "") {
        this.$message({
          showClose: true,
          message: "请选择收款账户",
          type: "error",
        });
        return;
      }
      params.append("account", this.selectparms.act);
      params.append("cpnid", this.selectparms.cpnID);
      params.append("type", "0");
      params.append(
        "date",
        this.selectparms.date == ""
          ? ""
          : new Date(this.selectparms.date).toLocaleDateString()
      );
      params.append(
        "edate",
        this.selectparms.edate == ""
          ? ""
          : new Date(this.selectparms.edate).toLocaleDateString()
      );
      this.selectparms.date =
        this.selectparms.date == ""
          ? ""
          : new Date(this.selectparms.date).toLocaleDateString();
      this.selectparms.edate =
        this.selectparms.edate == ""
          ? ""
          : new Date(this.selectparms.edate).toLocaleDateString();
      this.listLoading = true;
      backstage.GetRcdateLst(this, params);
    },
    downloadRcBatch(type) {
      var arr = this.multipleSelection;
      var dates = [];
      if (this.selectparms.date == "") {
        this.$message({
          showClose: true,
          message: "请选择日期",
          type: "error",
        });
        return;
      }
      if (this.selectparms.cpnID == "") {
        this.$message({
          showClose: true,
          message: "请选择企业",
          type: "error",
        });
        return;
      }
      var params = new URLSearchParams();
      params.append("account", this.selectparms.act);
      params.append("cpnid", this.selectparms.cpnID);
      params.append("type", type);
      params.append(
        "date",
        this.selectparms.date == ""
          ? ""
          : new Date(this.selectparms.date).toLocaleDateString()
      );
      params.append(
        "date",
        this.selectparms.edate == ""
          ? ""
          : new Date(this.selectparms.edate).toLocaleDateString()
      );
      this.selectparms.date =
        this.selectparms.date == ""
          ? ""
          : new Date(this.selectparms.date).toLocaleDateString();
      this.selectparms.edate =
        this.selectparms.edate == ""
          ? ""
          : new Date(this.selectparms.edate).toLocaleDateString();
      if (type == 0) {
        this.downloadRcBatchLoading = true;
      } else {
        this.downloadRcBatchERPLoading = true;
      }
      const loading = this.$loading({
        lock: true,
        text: "后台账单下载中,请勿关闭页面",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      backstage.downloadRcBatch(this, params, dates, type, loading);
    },
    downloadRc(row) {
      var params = new URLSearchParams();
      params.append("account", row.act);
      params.append("cpnid", this.selectparms.cpnID);
      params.append("type", "0");
      params.append("date", row.dtStr);
      this.downloadRcBatchLoading = true;
      const loading = this.$loading({
        lock: true,
        text: "后台账单下载中,请勿关闭页面",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });

      backstage.downloadRc(this, params, loading);
    },
    downloadRcERP() {
      if (this.selectparms.date == "") {
        this.$message({
          showClose: true,
          message: "请选择日期",
          type: "error",
        });
        return;
      }
      if (this.selectparms.cpnID == "") {
        this.$message({
          showClose: true,
          message: "请选择企业",
          type: "error",
        });
        return;
      }

      var year = new Date(this.selectparms.date).getFullYear();
      var month = (new Date(this.selectparms.date).getMonth() + 1).toString();
      var day = new Date(this.selectparms.date).getDate().toString();
      var sec = new Date(this.selectparms.date.toLocaleDateString());
      var now = new Date(new Date().toLocaleDateString());
      if (sec >= now) {
        this.$message({
          showClose: true,
          message: "只能下载昨日之前账单",
          type: "error",
        });
        return;
      }
      var day2 = "";
      if (day.length <= 1) {
        day2 = "0" + day;
      } else {
        day2 = day;
      }
      var month2 = "";
      if (month.length <= 1) {
        month2 = "0" + month;
      } else {
        month2 = month;
      }
      var lastmouthtime = new Date(this.selectparms.date);
      lastmouthtime = lastmouthtime.setDate(lastmouthtime.getDate() + 1);
      lastmouthtime = new Date(lastmouthtime);
      var lastday = lastmouthtime.getDate().toString();
      var lastmouth = (lastmouthtime.getMonth() + 1).toString();
      var lastyear = lastmouthtime.getFullYear();
      var day3 = "";
      if (lastday.length <= 1) {
        day3 = "0" + lastday;
      } else {
        day3 = lastday;
      }
      var month3 = "";
      if (lastmouth.length <= 1) {
        month3 = "0" + lastmouth;
      } else {
        month3 = lastmouth;
      }
      var name = year + month2 + day2 + "_" + lastyear + month3 + day3;
      this.downloadRcERPLoading = true;
      window.location.href =
        "https://wpaydp.s3.cn-northwest-1.amazonaws.com.cn/Alipay/cpn%3D" +
        this.selectparms.cpnID +
        "/year%3D" +
        year +
        "/month%3D" +
        month +
        "/day%3D" +
        day +
        "/" +
        name +
        ".csv";
      this.downloadRcERPLoading = false;
    },
  },
};
// 后台事件
var backstage = {
  // 获取MngrLog集合
  GetRcdateLst: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "GetRcdateLst",
      params,
      "post",
      function (res) {
        thisobj.listLoading = false;
        var body = res.data.data;
        thisobj.HeartbeatData = [];
        thisobj.HeartbeatData = body.rows;
        thisobj.listQuery.page = body.page;
        thisobj.listQuery.total = body.records;
      },
      function (err) {
        thisobj.listLoading = false;
      }
    );
  },
  GeCpntListItems: function (thisobj) {
    SelectAjax(
      "GeCpntListItems",
      function (response) {
        var body = response.data;
        thisobj.cpnoptions = [];
        body.data.forEach((element) => {
          thisobj.cpnoptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  GetCpnActList: function (thisobj) {
    SelectAjax(
      "GetCpnActList?val=&cpnid=" + thisobj.selectparms.cpnID + "",
      function (response) {
        var body = response.data;
        thisobj.cpnactlstoptions = [];
        if (body.data.length > 0) {
          thisobj.selectparms.act = body.data[0].value;
        }
        body.data.forEach((element) => {
          thisobj.cpnactlstoptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  GetPyChannelSelectListItems: function (thisobj, cpnid) {
    SelectAjax(
      "GetPyChannelSelectListItemsByCpnid?val=" + cpnid,
      function (response) {
        var body = response.data;
        thisobj.paytychanneloptions = [];
        body.data.forEach((element) => {
          thisobj.paytychanneloptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  GetCpnActListSelectListItems: function (thisobj, cpnid, paychannel) {
    SelectAjax(
      "GetCpnActList?cpnid=" + cpnid + "&paychannel=" + paychannel,
      function (response) {
        var body = response.data;
        thisobj.actlist = [];
        thisobj.export_form.uploadfiles = [];
        body.data.forEach((element) => {
          thisobj.actlist.push({
            value: element.value,
            label: element.text,
          });

          thisobj.export_form.uploadfiles.push({
            act: element.value,
            fillename: "",
            type: 0,
            label: element.text,
          });
        });
        // console.log(thisobj.export_form.uploadfiles);
      },
      function (error) {}
    );
  },
  downloadRcBatch: function (thisobj, params, datejson, type, loading) {
    AjaxToken(
      thisobj,
      "DownloadRcBatch?date=" +
        thisobj.selectparms.date +
        "&edate=" +
        thisobj.selectparms.edate +
        "&account=" +
        thisobj.selectparms.act +
        "&dates=" +
        JSON.stringify(datejson) +
        "&type=" +
        type +
        "&cpnid=" +
        thisobj.selectparms.cpnID +
        "",
      params,
      "get",
      function (res) {
        thisobj.downloadRcBatchLoading = false;
        thisobj.downloadRcBatchERPLoading = false;
        thisobj.downloadLoading = false;
        loading.close();
        var body = res.data;
        if (body.success) {
          if (body.data != "") {
            window.location.href = body.data;
          } else {
            thisobj.$message({
              showClose: true,
              message: body.message,
              type: "error",
            });
          }
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {
        loading.close();
        thisobj.downloadLoading = false;
      }
    );
  },
  downloadRc: function (thisobj, params, loading) {
    AjaxToken(
      thisobj,
      "IsDownloadRc?date=" +
        thisobj.selectparms.date +
        "&account=" +
        thisobj.selectparms.act +
        "&type=0&cpnid=" +
        thisobj.selectparms.cpnID +
        "",
      params,
      "get",
      function (res) {
        loading.close();
        thisobj.downloadRcBatchLoading = false;
        var body = res.data;
        if (body.success) {
          if (body.data != "") {
            window.location.href = body.data;
          } else {
            var url =
              commconfig.baseapi +
              "DownloadRc?date=" +
              thisobj.selectparms.date +
              "&account=" +
              thisobj.selectparms.act +
              "&type=0&cpnid=" +
              thisobj.selectparms.cpnID +
              "";
            window.location.href = url;
          }
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {
        thisobj.downloadLoading = false;
      }
    );
  },
  beforeUpload: function (that, params) {
    AjaxUpload(
      that,
      "OtherPayUploadFile",
      params,
      "post",
      function (res) {
        that.exportloading = false;
        if (res.data.success) {
          that.export_form.forecahfiles = [];
          that.$notify({
            title: "上传成功",
            message: res.data.message,
            type: "success",
          });
        } else {
          that.$notify.error({
            title: "上传失败",
            message: res.data.message,
          });
        }
      },
      function (error) {
        that.exportloading = false;
        that.$notify.error({
          title: "请求错误",
          message: error,
        });
      }
    );
  },
};
</script>

<style lang="scss" scoped>
.red {
  color: red;
}
.green {
  color: green;
}
.board-title {
  height: 60px;
  width: 100%;
  line-height: 60px;
  display: flex;
  background-color: rgba(204, 204, 204, 0.445);
  text-align: center;
  margin-left: 0px !important;
  margin-right: 0px !important;
}
.left-title {
  width: 33%;
  height: 60px;
}
.center-title {
  width: 34%;
  height: 60px;
}
.right-title {
  flex: 1;
  height: 60px;
}
::v-deep .el-upload-list {
  margin-top: 30px !important;
  width: 64% !important;
  padding-left: 5px !important;
  padding-bottom: 10px !important;
}
::v-deep.el-upload--text {
  width: 62% !important;
}
</style>
<style>
.exportotherpaydiv .el-form-item--medium .el-form-item__label {
  text-align: right;
  font-weight: 400;
  width: 100px;
}
.exportotherpaydiv .dialog-footer {
  text-align: right;
}
.el-dialog__title {
  font-size: 18px !important;
}

@media screen and (min-width: 1200px) {
  .upload-box {
    padding-left: 9%;
  }
}
@media screen and (max-width: 1199px) {
  .upload-box {
    padding-left: 0;
  }
}
.checkBoard {
  width: 100%;
  /* height:200px; */
  box-shadow: 0 0 1px 1px #ccc;
  z-index: 9999;
  background-color: #fff;
  border: 1px solid rgb(236, 236, 236);
  border-radius: 2px;
}
* {
  margin: 0px;
  padding: 0px;
  list-style: none;
}
.adminList {
  /* background-color: pink; */
  height: 100%;
  min-height: 40px;
}
.adminList li {
  font-size: 14px !important;
  line-height: 40px;
  text-align: center;
  display: flex;
}
.item {
  width: 33.3%;
}
.boardFoot {
  text-align: center;
  padding-bottom: 30px;
  margin-top: 30px;
}
</style>
